package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.components.calendar.api.UniqueElementList;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class DeptAudienceHelper extends NewAudienceHelper {
	private static final long serialVersionUID = 7348850597515892100L;

	public DeptAudienceHelper(ORMInterface orm) {
		List<Universities> univList = new UniversitiesDAO(orm)
				.getAllUniversities();
		List<Departments> cList = new ArrayList<Departments>();
		for (Universities univ : univList) {
			cList.addAll(new DepartmentsDAO(orm).getDeptForUniv(univ));
		}
		deptList.add(cList);
	}

	public List<String> getStringList() {
		List<String> list = new ArrayList<String>();
		List<IModel<Departments>> temp = deptList.getObject();
		for (IModel<Departments> t : temp) {
			Set<String> keys = roles.keySet();
			for (String r : keys)
				list.add("[" + r +" of " +t.getObject().getTitle() + "]; ");
		}
		return list;
	}

	public IModel<Departments> getDept(String s) {
		List<IModel<Departments>> ci = deptList.getObject();
		for (IModel<Departments> c : ci) {
			if (s.contains(c.getObject().getTitle())&& s.contains("[") && s.contains("]"))
				return c;
		}
		return null;
	}

	public CayenneList<Users> getAllUsers(String s) {
		IModel<Departments> ci = getDept(s);
		Set<Entry<String, Integer>> set = roles.entrySet();
		for (Entry<String, Integer> e : set) {
			if (s.contains(e.getKey())) {
				return new DeptDataHandler(ci).getPeopleByRole(e.getValue());
			}
		}
		return new CayenneList<Users>(null);
	}

	public CayenneList<GenericAudience> getAudiences(String s) {
		return null;
	}

	public static HashMap<String, Integer> roles = new HashMap<String, Integer>();
	static {
		roles.put("Administrators", UserDeptLink.PRIMARY_ADMIN);
		roles.put("Faculty", UserDeptLink.TEACHER);
		roles.put("Students", UserDeptLink.STUDENT);
	}

	private UniqueElementList<Departments> deptList = new UniqueElementList<Departments>(
			null);

	@Override
	public String getNameForAudience(String s) {
		Set<String> set = roles.keySet();
		for (String role : set) {
			if (s.contains(role)) {
				return role + ", " + getDept(s).getObject().getTitle();
			}
		}
		return "";
	}

	@Override
	public int getRoleId(String s) {
		Set<String> set = roles.keySet();
		for (String role : set) {
			if (s.contains(role)) {
				return roles.get(role);
			}
		}
		return 0;
	}
}
